﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Model;

public partial class MembershipCard_EditCard : BasePage
{
    Access access = new Access();
    MembershipCardBO mbo = new MembershipCardBO();
    StoreNameBO sbo = new StoreNameBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = sbo.GetStoreByToUserName(CurrentUserInfo.MID);
            foreach (DataRow dr in dt.Rows)
            {
                //ddlStore.Items.Add(new ListItem(dr["Name"].ToString(), dr["ID"].ToString()));
            }
            ddlStore.Items.Add(new ListItem("请选择店铺", "-1"));
            if (GetQeuryString("para") == "update")
            {
                InitData();
            }
            else if (GetQeuryString("para") == "add")
            {
                //this.txtCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (ddlStore.Items.Count > 0)
            {
                if (GetQeuryString("para") == "update")
                {
                    Model.MembershipCard menu = GetEntity();
                    menu.ID = Convert.ToInt32(GetQeuryString("id"));
                    menu.SID = int.Parse(ddlStore.SelectedValue);
                    menu.CID = int.Parse(ddlcity.SelectedValue);
                    menu.Explain = content1.InnerText;
                    mbo.Update(menu);
                    //StoreName store = new StoreName();
                    //store.CityName = ddlcity.SelectedItem.ToString();
                    //store.Name = ddlStore.SelectedItem.ToString();
                    //sbo.Update(store);
                    AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
                }
                else if (GetQeuryString("para") == "add")
                {
                    Model.MembershipCard m = GetEntity();
                    mbo.Add(m);
                    //StoreName store = new StoreName();
                    //store.CityName = ddlcity.SelectedItem.ToString();
                    //store.Name = ddlStore.SelectedItem.ToString();
                    //sbo.Update(store);
                    AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
                }
            }
            else
                AlertMessage("请先维护店铺");
        }
        catch (Exception)
        {
            AlertMessage("请将信息填写完整");
        }
    }

    private void InitData()
    {
        string id = GetQeuryString("id");
        MembershipCard ds = mbo.GetModel(int.Parse(id)) ;
        //DataRow dr = ds.Tables[0].Rows[0];
        //this.txtMenuName.Text = dr["MenuName"].ToString();
        //this.txtMenuIcon.Text = dr["MenuIcon"].ToString();
        //this.txtMenuUrl.Text = dr["MenuUrl"].ToString();
        //this.txtCreateTime.Text = Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd");
        //this.txtUpdateTime.Text = Convert.ToDateTime(dr["UpdateTime"]).ToString("yyyy-MM-dd");
        //this.txtSequnce.Text = dr["suqence"].ToString();
        ddlType.SelectedValue = ds.Type.ToString();
        txtTitle.Text = ds.Title;
        content1.InnerText = ds.Explain;
        ddlStore.SelectedValue = ds.SID.ToString();
        ddlcity.SelectedValue = ds.CID.ToString();
    }
    public Model.MembershipCard GetEntity()
    {
        Model.MembershipCard m = new Model.MembershipCard();
        m.CreateDate = DateTime.Now;
        m.Explain = content1.InnerText;
        m.Title = txtTitle.Text;
        m.Type = int.Parse(ddlType.SelectedValue);
        m.MID = CurrentUserInfo.MID;
        m.SID = int.Parse(ddlStore.SelectedValue);
        m.CID = int.Parse(ddlcity.SelectedValue);
        return m;
    }




    protected void ddlcity_SelectedIndexChanged(object sender, EventArgs e)
    {        
                ddlStore.Items.Clear();
                ddlStore.Items.Add(new ListItem("请选择店铺", "-1"));
                if (ddlcity.SelectedValue == "1")
                {
                    ddlStore.Items.Add(new ListItem("旗舰店","1"));
                    ddlStore.Items.Add(new ListItem("保定店","2"));
                    ddlStore.Items.Add(new ListItem("永银店","3"));
                    ddlStore.Items.Add(new ListItem("正大乐城店","4"));
                    ddlStore.Items.Add(new ListItem("联洋店","5"));
                }
                else if (ddlcity.SelectedValue == "2")
                {
                    ddlStore.Items.Add(new ListItem("金融街","1"));
                    ddlStore.Items.Add(new ListItem("凯德晶品","2"));
                    ddlStore.Items.Add(new ListItem("颐堤港","3"));
                    ddlStore.Items.Add(new ListItem("远洋店","4"));
                   
                }
                else if (ddlcity.SelectedValue == "3")
                {
                    ddlStore.Items.Add(new ListItem("印象城","1"));                 
                }

                else if (ddlcity.SelectedValue == "4")
                {
                    ddlStore.Items.Add(new ListItem("正方京城","1"));
                }
                else if (ddlcity.SelectedValue == "5")
                {
                    ddlStore.Items.Add(new ListItem("南阳店","1"));
                } 
    }
}